package cn.com.czpb.client.custorm.webkit;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;


/**
 * Created by zhousz on 2017/7/13.
 */

public class RbWebChromeClient extends WebChromeClient {

    public static final int REQUEST_FILE_PICKER = 1;

    private Activity act;
    //是否支持手势调整字体大小，true则支持，反之不支持，默认为true
    private boolean gestureTextSizeAdjust = true;
    private ValueCallback<Uri[]> mFilePathCallback;
    private RbLineProgressBar progressBar;

    public RbWebChromeClient(Activity activity) {
        act = activity;
    }

    public RbWebChromeClient(Activity activity, RbLineProgressBar progressBar) {
        this.act = activity;
        this.progressBar = progressBar;
    }

    @Override
    public void onProgressChanged(WebView view, int newProgress) {
        super.onProgressChanged(view, newProgress);
        RbLineProgressBar progressBar = getProgressBar();
        if (progressBar != null)
            progressBar.setProgress(newProgress);

//        //加载完成之后显示手势调整字体提示框
//        if (gestureTextSizeAdjust && newProgress == 100) {
//            RbSystemStore systemStore = RbSystemStore.get(act);
//            boolean isShowedTextZoom = systemStore.isWebViewTextZoomSettingShowed();
//            if (!isShowedTextZoom) {
//                RbWebViewTextZoomWarnDialog dialog = new RbWebViewTextZoomWarnDialog(act);
//                dialog.show();
//            }
//            systemStore.setWebViewTextZoomSettingShowed(true);
//        }
    }

    @Override
    public boolean onShowFileChooser(WebView webView,
                                     ValueCallback<Uri[]> filePathCallback,
                                     FileChooserParams fileChooserParams) {
        mFilePathCallback = filePathCallback;
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("*/*");
        act.startActivityForResult(Intent.createChooser(intent, "File Chooser"),
                REQUEST_FILE_PICKER);
        return true;
    }

    protected RbLineProgressBar getProgressBar(){
        return progressBar;
    }

    /**
     * 是否支持手势调整字体大小，true则支持，反之不支持
     */
    public boolean isGestureTextSizeAdjust() {
        return gestureTextSizeAdjust;
    }

    /**
     * 是否支持手势调整字体大小，true则支持，反之不支持，默认为true
     *
     * @param gestureTextSizeAdjust
     */
    public void setGestureTextSizeAdjust(boolean gestureTextSizeAdjust) {
        this.gestureTextSizeAdjust = gestureTextSizeAdjust;
    }

    public ValueCallback<Uri[]> getFilePathCallback() {
        return mFilePathCallback;
    }

    public void setFilePathCallback(ValueCallback<Uri[]> filePathCallback) {
        this.mFilePathCallback = filePathCallback;
    }
}
